import { defineStore } from 'pinia'
import { ref } from 'vue'
import { userGetInfoService } from '@/api/user'

export const useUserStore = defineStore(
  'bill-user',
  () => {
    const result=localStorage.getItem('USER_KEY')
    let token =result?JSON.parse(result):''
    function setToken(newToken) {
      localStorage.setItem('USER_KEY',JSON.stringify(newToken))
      console.log("token",JSON.parse(localStorage.getItem('USER_KEY')))
      token = newToken
      console.log('实际存储的token',token)
    }
    function removeToken() {
      localStorage.clear()
      token.value = ''
    }

    const user = ref({})
    const getUser = async () => {
      const res = await userGetInfoService()
      user.value = res.data.data
    }
    const setUser = (obj) => {
      user.value = obj
    }
    return {
      token,
      setToken,
      removeToken,
      user,
      getUser,
      setUser
    }
  }
)
